namespace BassettData.Robots.Runner
{
    using System;
    using System.Collections.Generic;
    using Castle.MicroKernel.Registration;
    using Castle.MicroKernel.SubSystems.Configuration;
    using Castle.Windsor;
    using Castle.Windsor.Installer;

    /// <summary>
    /// Repository for robot tasks: locates and enumerates the robots
    /// in the RobotWerke assembly.
    /// </summary>
    public class RobotTaskRepository : IWindsorInstaller
    {
        /// <summary>
        /// Performs the installation in the <see cref="T:Castle.Windsor.IWindsorContainer"/>.
        /// </summary>
        /// <param name="container">The container.</param>
        /// <param name="store">The configuration store.</param>
        public void Install(IWindsorContainer container, IConfigurationStore store)
        {
            // See http://stw.castleproject.org/Windsor.Registering-components-by-conventions.ashx
            container.Register(Classes
                .FromAssemblyNamed("RobotWerke")
                .BasedOn<IRobotTask>()
                .WithService
                .FromInterface());
        }

        /// <summary>
        /// Finds the robot tasks in the RobotWerke assembly.
        /// </summary>
        /// <returns>The robot tasks, ready to run.</returns>
        public IEnumerable<IRobotTask> FindRobotTasks()
        {
            using (var container = new WindsorContainer())
            {
                container.Install(FromAssembly.This());
                return container.ResolveAll<IRobotTask>();
            }
        }
    }
}
